package doublearraylist;

import java.util.ArrayList;
import java.util.List;

public class TraingleYang {
    public static void main(String[] args) {
        List<List<Integer>> retList = traingle(6);
        for (int i = 0; i < retList.size(); i++) {
            for (int j = 0; j < retList.get(i).size(); j++) {
                System.out.print(retList.get(i).get(j)+" ");
            }
            System.out.println();
        }
    }
    public static List<List<Integer>> traingle(int length) {
        List<List<Integer>> retList = new ArrayList<>();
        //二维数组的第一行
        List<Integer> firstList = new ArrayList<>();
        firstList.add(1);
        retList.add(firstList);
        //二维数组从第二行开始
        for (int i = 1; i < length; i++) {
            List<Integer> list = new ArrayList<>();
            //每行的第一个
            list.add(1);
            //每行的中间
            List<Integer> preList = retList.get(i - 1);
            for (int j = 1; j < i; j++) {
                int val1 = preList.get(j);
                int val2 = preList.get(j-1);
                list.add(val1+val2);
            }
            //每行的最后一个
            list.add(1);
            //把每行加入到ret数组中
            retList.add(list);
        }
        return retList;
    }
}
